package cn.yy.cloudpay;

import cn.yy.cloudpay.activity.BaseActivity;
import cn.yy.cloudpay.application.CloudPayApplication;
import cn.yy.cloudpay.listener.IKeyboardResultListener;
import cn.yy.cloudpay.listener.ITitlebarClickListener;
import cn.yy.cloudpay.util.Constant;
import cn.yy.cloudpay.util.KeyboardStringUtil;
import cn.yy.cloudpay.util.StringUtil;
import cn.yy.cloudpay.view.KeyboardLayout;
import cn.yy.cloudpay.view.TitlebarLayout;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import org.apache.commons.lang.StringUtils;

public class KeyboardActivity extends BaseActivity {

	private TextView mTvResult;
	private Context mContext;
	
	private KeyboardLayout keyboard;
	private TableLayout tableLayout;
	
	private TitlebarLayout titlebar;
	private RelativeLayout relativeLayout;
	
	private CloudPayApplication mApplication;
	private boolean mIsReturn;
	private String totalMoney;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_keyboard);
//		Log.d("TAG", "MainActivity------onCreate()");
		mContext = this;
		mApplication = (CloudPayApplication) getApplication();
		Intent intent = getIntent();
		totalMoney =  getIntent().getStringExtra("totalMoney");
		mIsReturn = intent.getBooleanExtra(Constant.PARAM_ISRETURN, false);
		initView();
	}


	private void initView() {
		relativeLayout = (RelativeLayout) findViewById(R.id.include1);
		titlebar = new TitlebarLayout(mContext, relativeLayout, "请输入金额");
		titlebar.setTitlebarClickListener(new ITitlebarClickListener() {
			@Override
			public void OnRetrunListener(View view) {
				finish();
			}
		});
		tableLayout = (TableLayout) findViewById(R.id.keyboard_layout);
		mTvResult = (TextView) findViewById(R.id.textview_main_summary);
		if(!StringUtils.isEmpty(totalMoney)){
			Spanned rs = KeyboardStringUtil.getDiaplayString(totalMoney);
			mTvResult.setText(rs);
		}else{
			mTvResult.setText(KeyboardStringUtil.getResetResult());
		}
		keyboard = new KeyboardLayout(tableLayout, mIsReturn);
		keyboard.setKeyboardResultListener(new IKeyboardResultListener() {
			
			@Override
			public void onResetKeyResult() {
				mTvResult.setText(KeyboardStringUtil.getResetResult());
			}
			
			@Override
			public void onNumberKeyResult(String number) {
				Spanned v = KeyboardStringUtil.getNumKeyResult(number);
				mTvResult.setText(v);
			}
			
			@Override
			public void onBackSpaceResult() {
				mTvResult.setText(KeyboardStringUtil.getBackSpaceResult());
			}
			
			@Override
			public void onConfirmResult() {
				if(!"￥0.00".equals(KeyboardStringUtil.getAmount())) {
//					OP_TYPE = 1;
					returnBack(Constant.KEYBOARD_OP_TYPE_PAY);
//					showDialog();
				}
			}
			@Override
			public void onRefund() { // 退款
//				OP_TYPE = 2;
				returnBack(Constant.KEYBOARD_OP_TYPE_RETURN);
//				showDialog();
			}
		});
	}
	
	/**
	 * 支付或退款打开选择支付方式窗口
	 * @param type 1 支付  2退款
	 */
	/*private void showDialog() {
		//打开扫描条码界面
		Intent intent = new Intent();
		if(OP_TYPE == 1){ // 支付
			intent.putExtra(Constant.PARAM_AMOUNT, KeyboardStringUtil.getAmount());
			intent.putExtra(Constant.PARAM_SUMMARY, KeyboardStringUtil.getSummary());
			intent.putExtra(Constant.PARAM_PAYTYPE, PAY_TYPE);
			intent.setClass(KeyboardActivity.this, ScanCodeActivity.class);
		}else{ // 退款
			intent.putExtra(Constant.PARAM_PAYTYPE, PAY_TYPE);
			intent.setClass(KeyboardActivity.this, ScanCodeRetundActivity.class);
		}
		startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
	}*/
	
	/**
	 * 返回结果处理
	 */
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
//        switch (requestCode) {
//		case SCANNIN_GREQUEST_CODE:
//			if(resultCode == RESULT_OK){
//				//后续处理
//			}
//			break;
//		}
//	}
	
	/**
	 * 返回上一个页面
	 */
	private void returnBack(int opType) {
//		Log.d("ScanCode", "点击确定按钮");
		Intent resultIntent = new Intent();
		resultIntent.putExtra(Constant.PARAM_OPTYPE, opType); 
		resultIntent.putExtra(Constant.PARAM_AMOUNT, KeyboardStringUtil.getAmount()); // 货币格式的金额
		resultIntent.putExtra(Constant.PARAM_SUMMARY, KeyboardStringUtil.getSummary()); // 金额字符串，单位为分
		KeyboardActivity.this.setResult(RESULT_OK, resultIntent);
		finish();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		Log.d("TAG", "mainactivity-----onResume");
		//重新进入页面，重置金额
		if(StringUtil.isEmpty(totalMoney)){
			mTvResult.setText(KeyboardStringUtil.getResetResult());
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
//		Log.d("TAG", "mainactivity----onPause");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}